package com.mediacloud.app.newsmodule.view;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Build.VERSION;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;

import com.gyf.immersionbar.ImmersionBar;
import com.mediacloud.app.model.fragment.BaseFragment;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.quanzi.recyclerview.GridSpacingItemDecoration;
import com.mediacloud.app.reslib.enums.XKey;
import com.mediacloud.app.share.R.dimen;
import com.mediacloud.app.share.R.id;
import com.mediacloud.app.share.R.layout;
import com.mediacloud.app.share.R.style;
import com.mediacloud.app.share.model.ShareGridItem;
import com.mediacloud.app.share.utils.ShareGridDataUtil;
import com.mediacloud.app.share.view.BadgeView;
import com.tencent.mmkv.MMKV;
import com.utils.DisplayUtil;

import java.util.List;

public class SharePopGridWindowV2 extends Dialog implements OnItemClickListener {
    private Context mContext;
    public GridAdaptor adaptor;
    public View contentView;
    public RecyclerView shareGrid;
    public OnItemClickListener listener;
    public boolean clickDismiss;
    public TextView cancel;

    private View dialogDarkMask;
    public SharePopGridWindowV2(Context context) {
        this(context, style.AppfacShareProgressDialogStyle);
    }

    public SharePopGridWindowV2(Context context, int style) {
        super(context, style);
        this.clickDismiss = true;
        this.mContext = context;
        this.contentView = LayoutInflater.from(this.mContext).inflate(this.getResLayoutId(), (ViewGroup) null);
        this.setContentView(this.contentView);
        if(MMKV.defaultMMKV().getBoolean(XKey.DARK_MODE,false)){
            dialogDarkMask = getLayoutInflater().inflate(com.mediacloud.app.reslib.R.layout.mask_darkmode, null);
            if (MMKV.defaultMMKV().getBoolean(XKey.DARK_MODE, false)) {
                WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.TYPE_APPLICATION,
                        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                        PixelFormat.TRANSLUCENT);
                this.addContentView(dialogDarkMask,lp);
            }
        }
        this.setFit(this.contentView);
        this.shareGrid = (RecyclerView) this.contentView.findViewById(id.shareGrid);
        this.adaptor = new GridAdaptor();
        this.shareGrid.setAdapter(this.adaptor);
        this.contentView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                boolean b = SharePopGridWindowV2.this.contentView.onTouchEvent(event);
                SharePopGridWindowV2.this.dismiss();
                return b;
            }
        });
        this.cancel = (TextView) this.contentView.findViewById(id.cancel);
        cancel.setIncludeFontPadding(false);
        this.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharePopGridWindowV2.this.cancel();
            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (this.getWindow() != null && this.getWindow().getAttributes() != null) {
            LayoutParams lp = this.getWindow().getAttributes();
            lp.width = -1;
            lp.height = -1;
            this.getWindow().setAttributes(lp);
            this.setTranslucentStatus();
        }

    }

    protected void setTranslucentStatus() {
        if (VERSION.SDK_INT >= 21) {
            this.getWindow().addFlags(-2147483648);
            this.getWindow().setStatusBarColor(0);
        } else {
            this.getWindow().addFlags(67108864);
        }

    }

    protected void setFit(View contentView) {
        View parent;
        for (View view = contentView; view.getParent() != this.getWindow().getDecorView(); view = parent) {
            parent = (View) view.getParent();
            parent.setFitsSystemWindows(this.getFitSystemWindow());
        }

    }

    protected boolean getFitSystemWindow() {
        return false;
    }

    public int getResLayoutId() {
        return layout.layout_gridstyle_sharewin;
    }

    public void setShareGirdListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void show(Activity activity) {
        this.shareGrid.setAdapter((Adapter) null);
        if (activity.getResources().getConfiguration().orientation == 2) {
            this.adaptor.islandScape = true;
            this.shareGrid.setLayoutManager(new LinearLayoutManager(activity, 0, false));
        } else {
            this.adaptor.islandScape = false;
            this.shareGrid.setLayoutManager(new GridLayoutManager(this.mContext, 5, 1, false));
            this.shareGrid.addItemDecoration(new GridSpacingItemDecoration(
                    5,
                    DisplayUtil.dip2px(this.mContext, 8f),
                    DisplayUtil.dip2px(this.mContext, 0f),
                    false
            ));
            if (ShareGridDataUtil.isNavigationBarExist(activity)) {
                int height = ImmersionBar.getNavigationBarHeight(activity);
                MarginLayoutParams params = (MarginLayoutParams) this.cancel.getLayoutParams();
                params.bottomMargin = height;
                this.cancel.setLayoutParams(params);
            }
        }

        this.shareGrid.setAdapter(this.adaptor);
        super.show();
    }

    public void setShareGridAdaptorData(List<ShareGridItem> data) {
        this.adaptor.gridItemList = data;
        ShareGridItem item = new ShareGridItem("举报", R.drawable.news_report_ico, "举报");
        if (!data.contains(item)) {
            adaptor.gridItemList.add(item);
        }
        this.adaptor.notifyDataSetChanged();
    }
    public void setShareGridDongTaiAdaptorData(List<ShareGridItem> data) {
        this.adaptor.gridItemList = data;
        this.adaptor.notifyDataSetChanged();
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (this.clickDismiss) {
            this.dismiss();
        }

        if (this.listener != null) {
            this.listener.onItemClick(parent, view, position, id);
        }

    }

    public void setAngle(int angle){
        if(contentView instanceof  RotateLayout){
            ((RotateLayout)contentView).setAngle(angle);
        }
    }
    public class GridAdaptor extends Adapter {
        public List<ShareGridItem> gridItemList = null;
        public boolean islandScape = false;

        protected GridAdaptor() {
        }
        @Override
        public int getItemCount() {
            return this.gridItemList == null ? 0 : this.gridItemList.size();
        }

        public ShareGridItem getItem(int position) {
            return this.gridItemList == null ? null : (ShareGridItem) this.gridItemList.get(position);
        }

        @NonNull
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(SharePopGridWindowV2.this.mContext).inflate(layout.sharegrid_itemmodule, (ViewGroup) null));
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    try {
                        int position = (Integer) v.getTag(id.share_item_rcy);
                        SharePopGridWindowV2.this.onItemClick((AdapterView) null, v, position, GridAdaptor.this.getItemId(position));
                    } catch (Exception var3) {
                        var3.printStackTrace();
                    }

                }
            });
            if (this.islandScape) {
                ViewGroup.LayoutParams layoutParams = new android.widget.LinearLayout.LayoutParams(parent.getContext().getResources().getDimensionPixelSize(dimen.dimen_70), -2);
                viewHolder.itemView.setLayoutParams(layoutParams);
            }

            return viewHolder;
        }
        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder hd, int position) {
            ViewHolder holder = (ViewHolder) hd;
            hd.itemView.setTag(id.share_item_rcy, position);
            holder.shareGridItemImg.setImageResource(this.getItem(position).resId);
            holder.shareGirdItemTxt.setText(this.getItem(position).label);
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            ImageView shareGridItemImg;
            TextView shareGirdItemTxt;
            BadgeView badgeView;

            public ViewHolder(View itemView) {
                super(itemView);
                this.shareGridItemImg = (ImageView) itemView.findViewById(id.shareGridItemImg);
                this.shareGirdItemTxt = (TextView) itemView.findViewById(id.shareGirdItemTxt);
                this.badgeView = new BadgeView(SharePopGridWindowV2.this.mContext);
                this.shareGirdItemTxt.setIncludeFontPadding(false);
            }
        }
    }
}
